$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_wgpj_m")
    const dgrid1 = $NG.getCmpApi("p_form_wgpj_d1")
    const getHeadValue = (str) => mstform.getItem(str).getValue()


    let config = {}

    if (page.oType == 'add') {
        getDetail()
    }

    useUpdateRows(({ args }) => {
        const rows = args[0]
        sumAmt(rows)
    }, "p_form_wgpj_d1")

    useValuesChange(() => {
        getCntCbInfoByPhid()
    }, "p_form_wgpj_m.u_fbhtmc")

    function sumAmt(rows){
        let a = 0
        let b = 0
        let c = 0
        rows.forEach(el => {
            if (el.u_pjyj.includes("合同金额(元)")) {
                el.u_nr = (Number(config.cntInfo?.user_fbhtje) || 0)
                a = (Number(el.u_nr) || 0)
            } else if (el.u_pjyj.includes("结算金额(元)")) {
                el.u_nr = (Number(config.jsAmt?.amt) || 0)
                b = (Number(el.u_nr) || 0)
            } else if (el.u_pjyj.includes("合同工期")){
                el.u_nr = (Number(config.cntInfo.user_jhgq) || 0)
                c = (Number(el.u_nr) || 0)
            } else if (el.u_pjyj.includes("实际工期")){
                d = (Number(el.u_nr) || 0)
            } 
        })
        const row = rows.filter(el => {
            console.log(el)
            if (el.u_pjyj.includes('金额差异')) {
                return el
            }
        })
        const row2 = rows.filter(el => {
            console.log(el)
            if (el.u_pjyj.includes('工期差异')) {
                return el
            }
        })
        console.log(row)
        row[0].u_nr = a - b
        row2[0].u_nr = d - c
        dgrid1.refreshView()
    }

    // 获取合同信息
    function getCntCbInfoByPhid() {
        const phid = getHeadValue("u_fbhtmc")
        if (!phid) {
            return
        }
        getJsdAmt(phid)
        $NG.execServer("getCntCbInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            const arr = [

                {
                    key: "u_fbhtbh",
                    value: datas.cnt_no
                },
                {
                    key: "u_ssxmb",
                    value: {
                        label: datas.phid_pc_exname,
                        value: datas.phid_pc
                    }
                },
                {
                    key: "u_fbsmc",
                    value: {
                        value: datas.fbsdw_phid,
                        label: datas.fbsdw_name
                    }
                },
                {
                    key: "u_xcfzr",
                    value: datas.user_fbsxcfzr
                }

            ]
            config.cntInfo = {...datas}
            const rows = dgrid1.getRows()
            sumAmt(rows)
            getProjectInfoByPhid(datas.phid_pc)
            getSupplyInfo(datas.fbsdw_phid)
            setHeadValue(arr)
        })
    }

    // 获取结算但金额
    async function getJsdAmt(phid){
        if(!phid) return 
            const res = await $NG.execServer("SELECT",{sql:`select u_bqjsk as amt from p_form_fbjs_m where u_fbhtmc = '${phid}'`})
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            config.jsAmt = {datas}
    }

    // 获取项目信息
    async function getProjectInfoByPhid(phid){
        if(!phid) return 
            const res = await $NG.execServer("getProjectInfoByPhid",{phid})
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            mstform.getItem("u_xmjlb").setValue({
                label:datas.cat_dept_name,
                value:datas.cat_dept
            })
            mstform.getItem("u_xmjl").setValue({
                label:datas.project_manager_name,
                value:datas.project_manager
            })
        }

    // 获取分包商单位
    function getSupplyInfo(phid) {

        if (!phid) return
        $NG.execServer("getSupplyInfo", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            const arr = [
                {
                    key: "u_fbxs",
                    value: datas.user_fbslb
                },
                {
                    key: "u_fddbr",
                    value: datas.person
                },
                {
                    key: "u_lxdh",
                    value: datas.user_fddbrlxdh
                }
            ]
            setHeadValue(arr)
        })
    }

    // 赋值
    function setHeadValue(datas) {
        datas.forEach(item => {
            try {
                mstform.getItem(item.key).setValue(item.value)
            } catch (err) {
                console.log(`err`, err, item);
            }
        })
    }

    // 获取对照表数据
    function getDetail() {
        $NG.execServer("SELECT", { sql: `select u_pjyj from p_form_wgpjdzb_d1` }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data).reduce((value, item) => {
                value.push({ ...item.extendObjects })
                return value
            }, [])
            dgrid1.clearRows().then(() => {
                dgrid1.addRows(datas)
            })
        })
    }
})